﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using EasyClientPlus;
using Mode;

namespace WinFormTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public ClientPlus client;
        private void Form1_Load(object sender, EventArgs e)
        {
            client = new ClientPlus();
            string ip = "127.0.0.1";
            int port = 5000;
            client._ip = ip;
            client._port = port;
            client.Connect += Client_Connect;
            client.DataReceived += Client_DataReceived;
            client.ReCon += Client_ReCon;
            if (client.InitClient())
            {
                this.Invoke(new MethodInvoker(delegate
                {
                    label1.Text = "连接正常";
                }));
            }
            else
            {
                this.Invoke(new MethodInvoker(delegate
                {
                    label1.Text = "连接失败";
                }));
            }

        }


        private void Client_ReCon()
        {
            this.Invoke(new MethodInvoker(delegate
            {
                label1.Text = "服务器断开，正在重连接";
            }));
           
        }

        private void Client_Close(object sender, EventArgs e)
        {
            this.Invoke(new MethodInvoker(delegate
            {
                label1.Text = "服务器关闭";
            }));

            
        }

        private void Client_DataReceived(SuperSocket.ClientEngine.PackageEventArgs<SuperSocket.ProtoBase.BinaryPackageInfo<short>> Data)
        {
         
        }

        private void Client_Connect(object sender, EventArgs e)
        {
            ///第一步实例化一个对象，可以是任何可以序列化的类
            MessageStr ms = new MessageStr();
            ms.Name = "美国总统";
            ms.Msg = "奥巴马";

            byte[] info = SerializationHelpers.ObjectToByte(ms);
            client.SendData(InformationTypes.AddEmployee, info);

            this.Invoke(new MethodInvoker(delegate
            {
                label1.Text = "已连接上服务器";
            }));

        }
    }
}
